import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.ListSelectionModel;


public class MainFrame extends JFrame {

	/**
	 * 
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table_contacts;
	private JTable table_week;
	private JTable table_day;
	Contacts contacts = new Contacts(this);
	private JButton btnEditContact;
	private JButton btnDeleteContact;
	int contactSelectedIndex;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	@SuppressWarnings("serial")
	public MainFrame() {
		setTitle("Jochen Stark - Photovoltaik Verwaltung");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0, 0, screen.width, screen.height-30);
		setExtendedState(Frame.MAXIMIZED_BOTH);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnDatei = new JMenu("Datei");
		menuBar.add(mnDatei);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel_planner = new JPanel();
		panel_planner.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_planner.setBounds(5, 5, (screen.width/10 * 8) - 5, screen.height-96);
		contentPane.add(panel_planner);
		panel_planner.setLayout(null);
		
		JTabbedPane tabbedPanel_planner = new JTabbedPane(JTabbedPane.TOP);
		tabbedPanel_planner.setBounds(5, 5, panel_planner.getWidth()-10 , panel_planner.getHeight()-10);
		panel_planner.add(tabbedPanel_planner);
		
		JPanel panel_week = new JPanel();
		tabbedPanel_planner.addTab("Wochenansicht", null, panel_week, null);
		panel_week.setBounds(5, 5, tabbedPanel_planner.getWidth()-5, tabbedPanel_planner.getHeight()-10);
		panel_week.setLayout(null);
		
		JScrollPane scrollPanel_week = new JScrollPane();
		scrollPanel_week.setBounds(5, 35, panel_week.getWidth()-10, panel_week.getHeight()-55);
		panel_week.add(scrollPanel_week);
		
		table_week = new JTable();
		table_week.setRowSelectionAllowed(false);
		table_week.setModel(new DefaultTableModel(
			new Object[][] {
				{"00:00", null, null, null, null, null, null, null},
				{"01:00", null, null, null, null, null, null, null},
				{"02:00", null, null, null, null, null, null, null},
				{"03:00", null, null, null, null, null, null, null},
				{"04:00", null, null, null, null, null, null, null},
				{"05:00", null, null, null, null, null, null, null},
				{"06:00", null, null, null, null, null, null, null},
				{"07:00", null, null, null, null, null, null, null},
				{"08:00", null, null, null, null, null, null, null},
				{"09:00", null, null, null, null, null, null, null},
				{"10:00", null, null, null, null, null, null, null},
				{"11:00", null, null, null, null, null, null, null},
				{"12:00", null, null, null, null, null, null, null},
				{"13:00", null, null, null, null, null, null, null},
				{"14:00", null, null, null, null, null, null, null},
				{"15:00", null, null, null, null, null, null, null},
				{"16:00", null, null, null, null, null, null, null},
				{"17:00", null, null, null, null, null, null, null},
				{"18:00", null, null, null, null, null, null, null},
				{"19:00", null, null, null, null, null, null, null},
				{"20:00", null, null, null, null, null, null, null},
				{"21:00", null, null, null, null, null, null, null},
				{"22:00", null, null, null, null, null, null, null},
				{"23:00", null, null, null, null, null, null, null},
			},
			new String[] {
				"Uhrzeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"
			}
		));
		table_week.getColumnModel().getColumn(0).setPreferredWidth(50);
		table_week.getColumnModel().getColumn(0).setMinWidth(50);
		table_week.getColumnModel().getColumn(0).setMaxWidth(50);
		table_week.setRowHeight((scrollPanel_week.getHeight())/25);
		scrollPanel_week.setViewportView(table_week);
		
		JTableHeader tableHeader_week = table_week.getTableHeader();
		tableHeader_week.setPreferredSize(new Dimension(0,(scrollPanel_week.getHeight())/25));
		tableHeader_week.setAlignmentX(CENTER_ALIGNMENT);
		tableHeader_week.setFont(new Font("Arial",Font.BOLD,12));
		
		JPanel panel_week_display = new JPanel();
		panel_week_display.setBounds(5, 5, panel_week.getWidth()-10,30);
		panel_week.add(panel_week_display);
		panel_week_display.setLayout(null);
		
		JLabel lblWoche = new JLabel("Woche vom: DD.MM.YYYY - DD.MM.YYYY");
		lblWoche.setBounds(0, 5, 260, 15);
		panel_week_display.add(lblWoche);
		
		JButton btnVorhWoche = new JButton("Vorherige");
		btnVorhWoche.setBounds(265, 1, 100, 23);
		panel_week_display.add(btnVorhWoche);
		
		JButton btnNaechstWoche = new JButton("N\u00E4chste");
		btnNaechstWoche.setBounds(370, 1, 100, 23);
		panel_week_display.add(btnNaechstWoche);
		
		JPanel panel_day = new JPanel();
		tabbedPanel_planner.addTab("Tagesansicht", null, panel_day, null);
		panel_day.setBounds(5, 5, tabbedPanel_planner.getWidth()-5, tabbedPanel_planner.getHeight()-10);
		panel_day.setLayout(null);
		
		JPanel panel_day_overview = new JPanel();
		panel_day_overview.setBounds(5, 35, panel_day.getWidth()/2 -10, panel_day.getHeight()-55);
		panel_day.add(panel_day_overview);
		panel_day_overview.setLayout(null);
		
		JScrollPane scrollPanel_day_overview = new JScrollPane();
		scrollPanel_day_overview.setBounds(0, 0, panel_day_overview.getWidth(),panel_day_overview.getHeight());
		panel_day_overview.add(scrollPanel_day_overview);
		
		table_day = new JTable();
		table_day.setModel(new DefaultTableModel(
			new Object[][] {
				{"00:00", null},
				{"01:00", null},
				{"02:00", null},
				{"03:00", null},
				{"04:00", null},
				{"05:00", null},
				{"06:00", null},
				{"07:00", null},
				{"08:00", null},
				{"09:00", null},
				{"10:00", null},
				{"11:00", null},
				{"12:00", null},
				{"13:00", null},
				{"14:00", null},
				{"15:00", null},
				{"16:00", null},
				{"17:00", null},
				{"18:00", null},
				{"19:00", null},
				{"20:00", null},
				{"21:00", null},
				{"22:00", null},
				{"23:00", null},
			},
			new String[] {
				"Uhrzeit", "Termin"
			}
		));
		table_day.getColumnModel().getColumn(0).setPreferredWidth(50);
		table_day.getColumnModel().getColumn(0).setMinWidth(50);
		table_day.getColumnModel().getColumn(0).setMaxWidth(50);
		table_day.setRowHeight((scrollPanel_day_overview.getHeight())/25);
		scrollPanel_day_overview.setViewportView(table_day);
		
		JTableHeader tableHeader_day = table_day.getTableHeader();
		tableHeader_day.setPreferredSize(new Dimension(0,(scrollPanel_day_overview.getHeight())/25));
		tableHeader_day.setAlignmentX(CENTER_ALIGNMENT);
		tableHeader_day.setFont(new Font("Arial",Font.BOLD,12));
		
		JPanel panel_day_display = new JPanel();
		panel_day_display.setBounds(5, 5, panel_day.getWidth()-10, 30);
		panel_day.add(panel_day_display);
		panel_day_display.setLayout(null);
		
		JLabel lblDay = new JLabel("Wochentag, DD.MM.YYYY");
		lblDay.setBounds(0, 5, 260, 15);
		panel_day_display.add(lblDay);
		
		JButton btnVorhTag = new JButton("Vorheriger");
		btnVorhTag.setBounds(265, 1, 100, 23);
		panel_day_display.add(btnVorhTag);
		
		JButton btnNaechstTag = new JButton("N\u00E4chster");
		btnNaechstTag.setBounds(370, 1, 100, 23);
		panel_day_display.add(btnNaechstTag);
		
		JPanel panel_day_details = new JPanel();
		panel_day_details.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_day_details.setBounds(panel_day.getWidth()/2, 35, panel_day.getWidth()/2 -5, panel_day.getHeight()-55);
		panel_day.add(panel_day_details);
		panel_day_details.setLayout(null);
		
		JLabel lblTermindetails = new JLabel("Termindetails");
		lblTermindetails.setBounds(10, 11, 150, 14);
		panel_day_details.add(lblTermindetails);
		
		JPanel panel_contacts = new JPanel();
		panel_contacts.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_contacts.setBounds(panel_planner.getWidth()+10, 5, (screen.width/10 * 2) - 10 , screen.height-96);
		contentPane.add(panel_contacts);
		panel_contacts.setLayout(null);
		
		JScrollPane scrollPanel_contacts = new JScrollPane();
		scrollPanel_contacts.setBounds(5, 35, panel_contacts.getWidth()-10, panel_contacts.getHeight()-40);
		panel_contacts.add(scrollPanel_contacts);
		
		table_contacts = new JTable(){
			public boolean isCellEditable(int rowIndex,int colIndex){
				return false;
			}
		};
		table_contacts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table_contacts.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Nachname", "Vorname", "Ort"
			}
		));
		table_contacts.setRowSelectionAllowed(true);
		
		scrollPanel_contacts.setViewportView(table_contacts);
		
		table_contacts.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
		    @Override
		    public void valueChanged(ListSelectionEvent e)
		    {
		        if (!e.getValueIsAdjusting())
		        {
		            boolean rowsAreSelected = table_contacts.getSelectedRowCount() == 1;
		            contactSelectedIndex = table_contacts.getSelectedRow();
		            btnEditContact.setEnabled(rowsAreSelected);
		            btnDeleteContact.setEnabled(rowsAreSelected);
		        }
		    }
		});
		
		JPanel panel_contacts_buttons = new JPanel();
		panel_contacts_buttons.setBounds(5, 5, panel_contacts.getWidth()-10, 30);
		panel_contacts.add(panel_contacts_buttons);
		panel_contacts_buttons.setLayout(null);
		
		JButton btnAddContact = new JButton("Hinzuf\u00FCgen");
		btnAddContact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				NewContactDialog nc = new NewContactDialog(contacts);
				nc.setVisible(true);
			}
		});
		btnAddContact.setBounds(5, 0, panel_contacts_buttons.getWidth()/3-10, 23);
		panel_contacts_buttons.add(btnAddContact);
		
		btnEditContact = new JButton("Bearbeiten");
		btnEditContact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				EditContactDialog ec = new EditContactDialog(contacts,contactSelectedIndex);
				ec.setVisible(true);
			}
		});
		btnEditContact.setEnabled(false);
		btnEditContact.setBounds(panel_contacts_buttons.getWidth()/3+5, 0, panel_contacts_buttons.getWidth()/3-10, 23);
		panel_contacts_buttons.add(btnEditContact);
		
		btnDeleteContact = new JButton("L\u00F6schen");
		btnDeleteContact.setEnabled(false);
		btnDeleteContact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DeleteContactDialog dc = new DeleteContactDialog(contacts,contactSelectedIndex);
				dc.setVisible(true);
			}
		});
		btnDeleteContact.setBounds((panel_contacts_buttons.getWidth()/3)*2+5, 0,panel_contacts_buttons.getWidth()/3-10 , 23);
		panel_contacts_buttons.add(btnDeleteContact);

		contacts.retrieve_all_contacts();
		contacts.updateContactTable();
	}
	
	public JButton getBtnEditContact() {
		return btnEditContact;
	}
	
	public JButton getBtnDeleteContact() {
		return btnDeleteContact;
	}
	
	public JTable getTableContacts() {
		return table_contacts;
	}
}
